<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
include_once("includes/util.php");

/**
 * This function used as block function of smarty.
 * Purpose: check the category is in a list or not
 */
function is_in_list($params, $content, &$smarty, &$repeat) { 
	if (isset($content)) { 
  		$Id = $params['id']; 
    	$selId = $params['select_id'];
	  	foreach($selId as $item) {
	  		if($Id == $item)
	  			return $content;
	  	}
  	return ""; 
  } 
}

class BgcmsWebshop extends BgcmsProcess {
	
	var $db;
 	var $smarty;
 	
 	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	public function process_productgroup($sub1) {
		$ret="";
		if(isset($_POST['act'])) {
			if($_POST['act'] == 'createproductgroup') {
				BWebshopProductGroup::CreateProductGroup();
			} else if($_POST['act'] == 'delProductgroup') {
				BWebshopProductGroup::DeleteProductGroup();
			} else if($_POST['act'] == 'editProductgroup') {
				BWebshopProductGroup::EditProductGroup();
			}
			$this->process_product('product');
		} else {
			if(isset($_GET['submod']) && $_GET['submod'] == 'GetProductByPg') {
				$this->process_product('product');
				$products=BWebshopProduct::GetProductByGroupId($_REQUEST['groupid']);
				$this->smarty->assign("PRODUCT_LIST_BY_PRODUCT_GROUP", $products);	
			}else if($sub1 == "productgroup" && isset($_GET['id'])) {
				$productlistID = $_GET['id'];
	
				$productgroup = BWebshopProductGroupBean::get_product_list_by_id($this->db, $productlistID);
				$this->smarty->assign("ARR_PRODUCT_GROUP", $productgroup);
			} else {
				$countgroup =count(BWebshopProductGroupBean::get_all_product_group($this->db,$_GET['sort'],$_GET['by']));
				$group['data'] = BWebshopProductGroupBean::get_all_product_group_paging($this->db,ADMINPAGING,"list",$_GET['sort'],$_GET['by']);
				$result = BgcmsPaging::do_paging($countgroup,$group, ADMINPAGING, "?module=BgcmsWebshop&sub1=productgroup","list");
				$this->smarty->assign("ARR_PRODUCT_GROUP", $result);
			}		
		}
		
	}
	
	
	public function process_product($sub1) {
		if(isset($_POST['act'])) {
			$action = $_POST['act'];
			if($action == "addproduct") {
				BWebshopProduct::CreateProduct();
			} else if($action == "delproduct") {
				BWebshopProduct::Delete_Product();
			} else if($action == "editproduct") {
				BWebshopProduct::Edit_Product();
			} else if($action == "delphoto") {
				BWebshopProduct::Delete_Photo();
			}	
		}	
		$act = "";
		if(isset($_GET["act"])) {
			$act = $_GET["act"];
		}
		if($act == "newproduct" || $act=="editproduct") {
			$productList = BWebshopProductGroupBean::get_all_product_group($this->db,'','');
			$this->smarty->assign("PRODUCTLIST", $productList);

			if($act == "editproduct") {
				$id = "-1";
				if(isset($_GET['id'])) {
					$id = $_GET['id'];
				}
				$photos = BWebshopProductBean::PhotosByProductId($this->db, $id);
				$getproductone = BWebshopProductBean::getProduct($this->db, $id);
				$this->smarty->assign("PATH","../".UPLOAD_PRODUCT_THUMB);
				$this->smarty->assign("PHOTOS",$photos);
				$this->smarty->assign("PRODUCT",$getproductone);
			}
			//$imgs = upload::getUploadImages();
			//$this->smarty->assign("IMAGES", $imgs);
			
			$currency = BWebshopProductBean::getAllCurrency($this->db, null);
			$this->smarty->assign("CURRENCY", $currency);
		} else {
			$countproduct =count(BWebshopProductBean::getAllProduct($this->db,$_GET['sort'],$_GET['by']));
			$product['data'] = BWebshopProductBean::getAllProductsSharePage($this->db,ADMINPAGING,"list",$_GET['sort'],$_GET['by']);
			$result = BgcmsPaging::do_paging($countproduct,$product, ADMINPAGING, "?module=BgcmsWebshop&sub1=product","list");
			$this->smarty->assign("PRODUCTS", $result);
			$currency = BWebshopProductBean::getAllCurrency($this->db, null);
			$this->smarty->assign("CURRENCY", $currency);
		}
	}


	public function process_special($sub1) {
		if(isset($_POST['act'])) {
			if($_POST['act'] == 'addspecial') {
				BWebshopSpecialProduct::CreateSpecialProduct();
			} elseif($_POST['act'] == 'deletespecial') {
				BWebshopSpecialProduct::DeleteSpecialProduct();
			} elseif($_POST['act'] == 'editspecial') {
				BWebshopSpecialProduct::EditSpecialProduct();
			} elseif($_POST['act'] == 'change_position') {
				BWebshopSpecialProduct::change_position();		
			} elseif($_POST['act'] == 'getimage') {
				$getproductone = BWebshopProductBean::getProduct($this->db, $_POST['id']);
				$arr['Ok'] = "Ok";	
				$arr['image'] = "../".UPLOAD_PRODUCT_THUMB.$getproductone->product_thumbnail;	
				echo array2json($arr);
			}
		} else {	
			if(isset($_GET['message'])) {
				$msg = "";
				switch($_GET['message']) {
					case 1:
						$msg = _("Special updated.");
						break;
					case 2:
						$msg = _("Special not updated. Maybe you have not change anything.");
						break;
					case 99:
						$msg = $_SESSION['error_msg'];
						unset($_SESSION['error_msg']);
						break;
				}
				
				$this->smarty->assign("specials_message", $msg);
			}
			if($_GET['act'] == "editspecial"){
				$specialId = '';	
				if(isset($_GET['id']))
					$specialId = $_GET['id'];

				
				$productspecial = BWebshopProductBean::getProductGroup($this->db, null);
				$this->smarty->assign("PRODUCTS", $productspecial);
				
				$special = BWebshopSpecialProductBean::getSpecialBySpecialId($this->db, $specialId);
				$special->product_thumbnail = "../".UPLOAD_PRODUCT_THUMB.$special->product_thumbnail;
				$this->smarty->assign("THESPECIAL", $special);
			} else if($_GET['act'] == "position"){
				$id = $_GET['id'];
				$special = BWebshopSpecialProductBean::getSpecialBySpecialId($this->db, $id);
				$listspecial = BWebshopSpecialProductBean::get_special_by_level($this->db,$id);
				$this->smarty->assign("THESPECIAL", $special);
				$this->smarty->assign("SPECIALLEVEL", $listspecial);	
			} else {
				$productspecial = BWebshopProductBean::getProductGroup($this->db, null);
				$this->smarty->assign("PRODUCTS", $productspecial);
				// get all Specials Product in Admin
				$this->smarty->assign("SPECIALS", BWebshopSpecialProductBean::getAllSpecials($this->db,$_GET['sort'],$_GET['by']));
			}
		}
	}
	
	/* Author	:
 	 * Desc		: View + create new + delete + edit Payment Method
 	 */
	 
	public function process_payment_method($sub1)
	{
		$ret="";
		if(isset($_POST['act']))
		{
			if($_POST['act'] == 'addpaymentmethod')
			{
				BWebshopPaymentMethod::CreatePaymentMethod();
			}
			else if($_POST['act'] == 'deletepaymentmethod')
			{
				BWebshopPaymentMethod::DeletePaymentMethod();
			}
			else if($_POST['act'] == 'editpaymentmethod')
			{
				BWebshopPaymentMethod::EditPaymentMethod();
			}
		} else {
			if($sub1 == "payment_method" && isset($_GET['id']))
			{
				if(isset($_GET['id']))
					$id = $_GET['id'];

				$payment = BWebshopPaymentMethodBean::getPaymentMethodById($this->db, $id);
				$this->smarty->assign("PAYMENTMETHODONE", $payment);

			} else {
				$payment = BWebshopPaymentMethodBean::getAllPaymentMethod($this->db, null);
				$this->smarty->assign("PAYMENTMETHOD", $payment);
			}			
				
		}
	}	
	
	public function process_featured($sub1)
	{
		$ret="";
		if(isset($_POST['act']))
		{
			if($_POST['act'] == 'addfeatured')
			{
				BWebshopFeaturedProduct::CreateFeaturedProduct();
			}
			else if($_POST['act'] == 'deletefeatured')
			{
				BWebshopFeaturedProduct::DeleteFeaturedProduct();
			}
			else if($_POST['act'] == 'editfeatured')
			{
				BWebshopFeaturedProduct::EditFeaturedProduct();
			} elseif($_POST['act'] == 'change_position') {
				BWebshopFeaturedProduct::change_position();		
			} elseif($_POST['act'] == 'getimage') {
				$getproductone = BWebshopProductBean::getProduct($this->db, $_POST['id']);
				$arr['Ok'] = "Ok";	
				$arr['image'] = "../".UPLOAD_PRODUCT_THUMB.$getproductone->product_thumbnail;	
				echo array2json($arr);
			}
		} else {
			if(isset($_GET['message']))
			{
				$msg = "";	
			
				switch($_GET['message'])
				{
					case 1:
						$msg = _("Featured updated.");
						break;
					case 2:
						$msg = _("Featured not updated. Maybe you have not change anything");
						break;
					case 99:
						$msg = $_SESSION['error_msg'];
						unset($_SESSION['error_msg']);
						break;
				}
				
				$this->smarty->assign("featured_message", $msg);
			}
			if($sub1 == "featured" && $_GET['act']=="editfeatured" && isset($_GET['id']))
			{
				$featuredId = '';	
				if(isset($_GET['id']))
					$featuredId = $_GET['id'];

				$productfeatured = BWebshopProductBean::getProductGroup($this->db, null);
				$this->smarty->assign("PRODUCTS", $productfeatured);
				
				$featured = BWebshopFeaturedProductBean::getFeaturedByFeaturedId($this->db, $featuredId);
				$featured->product_thumbnail = "../".UPLOAD_PRODUCT_THUMB.$featured->product_thumbnail;
				$this->smarty->assign("THEFEATURED", $featured);
			} else if($_GET['act'] == "position"){
				$id = $_GET['id'];
				$featured = BWebshopFeaturedProductBean::getFeaturedByFeaturedId($this->db, $id);
				$listfeatured = BWebshopFeaturedProductBean::get_featured_by_level($this->db,$id);
				$this->smarty->assign("THEFEATURED", $featured);
				$this->smarty->assign("FEATUREDLEVEL", $listfeatured);	
			} else {
				$this->smarty->assign("FEATURED", BWebshopFeaturedProductBean::getAllFeatured($this->db,$_GET['sort'],$_GET['by']));
				
				$productfeatured = BWebshopProductBean::getProductGroup($this->db, null);
				$this->smarty->assign("PRODUCTS", $productfeatured);
			}			
				
		}
	}	
	
	
	public function process_shipping_method($sub1) {
		/*
		 * Save and build JSON object with newly added information
		 * and return to client
		 * 
		 */
		$ret="";
		if(isset($_POST['act'])) {
			if($_POST['act'] == 'addshippingmethod') {
				BWebshopShippingMethod::CreateShippingMethod();
			} else if($_POST['act'] == 'delShippingMethod') {
				BWebshopShippingMethod::DeleteShippingMethod();
			} else if($_POST['act'] == 'editshippingmethod') {
				BWebshopShippingMethod::EditShippingMethod();
			}
		} else {
			if(isset($_GET['message'])) {
				$msg = "";
				switch($_GET['message'])
				{
					case 1:
						$msg = _("Shipping method updated.");
						break;
					case 2:
						$msg = _("Shipping method not updated. Maybe you have not change anything");
						break;
					case 99:
						$msg = $_SESSION['error_msg'];
						unset($_SESSION['error_msg']);
						break;
				}
				
				$this->smarty->assign("shippingmethod_message", $msg);
			}
			if($sub1 == "shippingmethod" && isset($_GET['id']))
			{
				$ShippingMethodCode = $_GET['id'];

				$shippingmethod = BWebshopShippingMethodBean::getShippingMethodsByCode($this->db, $ShippingMethodCode);
				$this->smarty->assign("SHIPPINGMETHOD", $shippingmethod);
				
				$currency = BWebshopProductBean::getAllCurrency($this->db);
				$this->smarty->assign("CURRENCY", $currency);
			}
			else
			{
				$this->smarty->assign("SHIPPINGMETHOD", BWebshopShippingMethodBean::getAllShippingMethods($this->db));
				
				$currency = BWebshopProductBean::getAllCurrency($this->db);
				$this->smarty->assign("CURRENCY", $currency);
			}		
				
		}
	}
	
	public function process_incoming_order($sub1){
			/*
		 * Save and build JSON object with newly added information
		 * and return to client
		 * 
		 */
		$ret="";
		if(isset($_POST['act'])) {
			if($_POST['act'] == 'delincomingorder') {
				BWebshopInComingOrder::DeleteInComingOrder();
			} elseif($_POST['act'] == 'editincomingorder') {
				BWebshopInComingOrder::EditInComingOrder();
			} elseif($_POST['act'] == 'changepaid') {
				BWebshopInComingOrder::ChangePaidStatus();
			} elseif($_POST['act'] == 'sendPDF') {
				BWebshopInComingOrder::SendPDF();
			}
		} else {
			if(isset($_GET['message'])) {
				$msg = "";
				switch($_GET['message'])
				{
					case 1:
						$msg = _("Incoming Order updated.");
						break;
					case 2:
						$msg = _("Incoming Order not updated. Maybe you have not change anything");
						break;
					case 99:
						$msg = $_SESSION['error_msg'];
						unset($_SESSION['error_msg']);
						break;
				}
				
				$this->smarty->assign("incomingorder_message", $msg);
			}
			if($sub1 == "incomingorder" && isset($_GET['id'])) {
				$order_id = $_GET['id'];
				
				$country = BWebshopCountryBean::get_all_country($this->db);
				$single_order = BWebshopInComingOrderBean::get_incoming_order_by_id($this->db, $order_id);
				$orderinfo_product = BWebshopInComingOrderBean::get_incoming_order_product($this->db, $single_order->so_id);
				$single_order->product = $orderinfo_product;
				$this->smarty->assign("ALLCOUNTRY", $country);
				$this->smarty->assign("INCOMING_ORDER", $single_order);
			} else {
				$order = array();
				$countorderinfo =count(BWebshopInComingOrderBean::get_all_incoming_order($this->db));
				$orderinfo = BWebshopInComingOrderBean::get_all_incoming_order_paging($this->db,ADMINPAGING,"list",$_GET['sort'],$_GET['by']);
				if($orderinfo!=""){
					foreach($orderinfo as $item){
						$orderinfo_product = BWebshopInComingOrderBean::get_incoming_order_product($this->db, $item->so_id);
						$item->product = $orderinfo_product;
					}
				}
				$order['data'] = $orderinfo;
				$result = BgcmsPaging::do_paging($countorderinfo,$order, ADMINPAGING, "?module=BgcmsWebshop&sub1=incomingorder","list");
				$this->smarty->assign("INCOMING_ORDER", $result);
			}		
				
		}
	}
	
	public function process_email_receiver($sub1){
			/*
		 * Save and build JSON object with newly added information
		 * and return to client
		 * 
		 */
		 if(isset($_POST['act'])) {
			if($_POST['act'] == 'adduser') {
				$id = BWebshopEmailReceiverBean::save_email_receiver($this->db,$_POST['user_id']);
				$user=BgcmsUserBean::get_user_by_id($this->db,$_POST['user_id']);
				if($id >=0 )
				$arr = array("code" => 0, 
							"message" => _("New Email Receiver has been added."), 
							"user_name" => $user->user_name, 
							"user_email" => $user->user_email, 
							"is_admin" => $user->is_admin, 
							"id" => $id, 
							"user_id" => $_POST['user_id']);
				echo array2json($arr);
			}elseif($_POST['act'] == 'deluser'){
				$user=BgcmsUserBean::get_user_by_id($this->db,$_POST['user_id']);
				$res=BWebshopEmailReceiverBean::delete_email_receiver_by_id($this->db,$_POST['id']);
				if($res == true) {
					$arr = array("code" => 0, 
							"message" => _("Email Receiver has been delete."), 
							"user_name" => $user->user_name, 
							"user_email" => $user->user_email, 
							"is_admin" => $user->is_admin, 
							"user_id" => $_POST['user_id']);
				} else {
					$arr['code'] = -1;
					$arr['message'] = sprintf(_("Could not delte user <u>%s</u>."), $id);
				}
				echo array2json($arr);
			}
		} else {
			
			$data['user'] = BWebshopEmailReceiverBean::get_all_user_email($this->db);
			$data['allemail'] = BWebshopEmailReceiverBean::get_all_email($this->db);
			$this->smarty->assign("ALLEMAIL", $data);
		}
		 
	}	 
	
	public function process_settings($sub1) {
		$settings = BWebshopSettingsBean::get_all_settings($this->db);
		if ($_POST['act'] == "updatesettingsdata") {
			$settings = array_intersect_key($_POST, $settings);
			BWebshopSettingsBean::update_all_settings($this->db, $settings);
		} 	
		$this->smarty->assign("SHOP_SETTINGS", $settings);
	}
	
	public function process() {
		$sub1 = 'productgroup';
		if(isset($_GET['sub1'])) {
			$sub1 = $_GET['sub1'];
		}
		if($sub1 == "product") {
			$this->process_product($sub1);
		} else if($sub1 == "productgroup") {
			$this->process_productgroup($sub1);		
		} else if($sub1 == "featured") {
			$this->process_featured($sub1);	
		} else if($sub1 == "payment_method") {
			$this->process_payment_method($sub1);				
		} else if($sub1 == "specials" || $sub1 == "editspecial") {
			$this->process_special($sub1);			
		} else if($sub1 == "shippingmethod") {
			$this->process_shipping_method($sub1);			
		} else if($sub1 == "customer") {
			$cus = new BWebshopCustomer($this->db, $this->smarty);
			$cus->process();
		} else if($sub1 == "email_receiver") {
			$this->process_email_receiver($sub1);			
		} else if($sub1 == "incomingorder") {
			$this->process_incoming_order($sub1);
		} else if($sub1 == "settings") {
			$this->process_settings($sub1);
		}
		$this->Run();
	}
}
?>
